//
//  ShouCangViewController.m
//  news
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ShouCangViewController.h"
#import "PrefixHeader.pch"
@interface ShouCangViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *arr;
@end

@implementation ShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    /** 查询数据库 **/
    self.arr = [[DataBaseManager shareInstance]select];
    // 数组为空的时候弹出提示框
    if (_arr.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"亲~ 没有收藏哦!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }

    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    // 重用池
    [_tableView registerClass:[ShouCangTableViewCell class] forCellReuseIdentifier:@"cell"];
    // 注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shoucang) name:@"hhh" object:nil];
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)shoucang{
    self.arr = [[DataBaseManager shareInstance]select];
        [_tableView reloadData];
   }
#pragma mark - 删除确认信息设置
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
#pragma mark - 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除收藏
        [[DataBaseManager shareInstance]deleteMovieWithName:_arr[indexPath.row]];
        
        [_arr removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade ];
        
        // 删除以后数组为空的时候弹出提示框
        if (_arr.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"亲~ 没有收藏哦!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }
}
// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
    NSString *temp = _arr[sourceIndexPath.row];
    
    [_arr removeObjectAtIndex:sourceIndexPath.row];
    [_arr insertObject:temp atIndex:destinationIndexPath.row];
}

#pragma mark - tableView协议设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShouCangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.lable.text = [_arr[indexPath.row] listenName];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
// cell 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadXqViewController *read = [[ReadXqViewController alloc]init];
    read.name = [_arr[indexPath.row] listenName];
    read.docid = [_arr[indexPath.row] pubdate];
    [self.navigationController pushViewController:read animated:YES];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
